/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'id',
        title: 'ID',
        type: 'number',
        columnClass: 'text-center width-100',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq'
    },
    {
        id: 'name',
        title: '用户名',
        type: 'string',
        columnClass: 'text-center width-100',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'stockName',
        title: '股票名称',
        type: 'string',
        columnClass: 'text-center width-100',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'stockNum',
        title: '股票数量',
        type: 'string',
        columnClass: 'text-center width-100',
        fastSort: true,
        fastQuery: false,
    },
    {
        id: 'profit',
        title: '盈利分配',
        type: 'string',
        columnClass: 'text-center width-100',
        fastSort: true,
        fastQuery: false,
    },
    {
        id: 'publishTime',
        title: '发布时间',
        type: 'date',
        format:'yyyy-MM-dd',
        columnClass: 'text-center width-100',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'operation',
        title: '管理操作',
        type: 'string',
        columnClass: 'text-center width-200',
        fastSort: false,
        extra: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var html = "",
                edit, publish, back, del;
            if ($("#traderEdit") && $("#traderEdit").val() == 1) {
                edit = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
            }
            if ($("#traderPublish") && $("#traderPublish").val() == 1) {
                publish = "<a href='javascript:;' class='btn btn-primary btn-sm m-r-10' onclick='OperateHandle.pubInfo(" + record.id + ",\"" + record.name + "\")'><i class='fa fa-paper-plane-o'></i>&nbsp;发布&nbsp;</a>";
            }
            if ($("#traderBack") && $("#traderBack").val() == 1) {
                back = "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='OperateHandle.backInfo(" + record.id + ",\"" + record.name + "\")'><i class='fa fa-reply-all'></i>&nbsp;撤回&nbsp;</a>";
            }
            if ($("#traderDel") && $("#traderDel").val() == 1) {
                del = "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='OperateHandle.delInfo(" + record.id + ",\"" + record.name + "\")'><i class='fa fa-trash-o'></i>&nbsp;删除&nbsp;</a>";
            }
            if (record.status != 2) {
                html += edit + publish + del;
            } else if (record.status == 2) {
                html += back;
            }
            return html;
        }
    }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'trader/json/list',
    exportFileName: '操盘达人',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["id"],String:["name","stockName"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
grid.sortParameter.columnId = ['asc_id'];
//列表结束

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {
        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['lk_stockName_or_like_name'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }
    var addInfo = {
        initAddModal: function () {
            $addForm = $("#addForm");
            $addForm.find('input[name="name"]').val("");
            $addForm.find('input[name="stockName"]').val("");
            $addForm.find('input[name="stockNum"]').val("");
            $addForm.find('input[name="profit"]').val("");
            $(".alert-danger").remove();
            $addForm.psly().reset();
        }
    }
    //添加微信事件时显示时
    $('#addModal').on('show.bs.modal', function (event) {
        addInfo.initAddModal();});
    /**
     * 发布
     */
    function _pubInfo(id, content) {
        var tpl = '您是否确定发布 <strong>' + content + '</strong> ?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "trader/json/publish",
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "发布操作"
        });
    }
    /**
     * 撤回
     */
    function _backInfo(id, content) {
        var tpl = '您是否确定撤回<strong>' + content + '</strong> ?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "trader/json/back",
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "撤回操作"
        });
    }

    $('#editModal').on('show.bs.modal', function (event) {
            //获取接受事件的元素
            var button = $(event.relatedTarget),
            //获取data 参数
            datano = button.data('no'),
            modal = $(this),
            //获取列表框中的原始数据
            gridData = grid.sortOriginalDatas[datano],
            editForm = $("#editForm");
            modal.find('input[name="id"]').val(gridData.id);
            modal.find('input[name="name"]').val(gridData.name);
            modal.find('input[name="stockName"]').val(gridData.stockName);
            modal.find('input[name="stockNum"]').val(gridData.stockNum);
            modal.find('input[name="profit"]').val(gridData.profit);
        //清除错误提示
        editForm.psly().reset();
        $(".alert-danger").remove()
    });
    /**
     * 删除
     */
    function _delInfo(id, content) {
        var tpl = '您选择对 <strong>' + content + '</strong> 进行删除操作，删除后将无法恢复。<br/>您确定要进行删除操作吗?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "trader/json/del",
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "删除操作"
        });
    }

    //外部可调用
    return {
        bindEvent: _bindEvent,
        //resetPwd: resetPwd,
        delInfo: _delInfo,
        backInfo: _backInfo,
        pubInfo: _pubInfo
    }
}();
//操作处理结束

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});